package com.itheima.youdianda.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.mapper.YddUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author YUAN
* @description 针对表【ydd_user】的数据库操作Service实现
* @createDate 2022-09-06 18:36:39
*/
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser>
    implements YddUserService{

    /**
     * 用户管理分页查询
     * @param page
     * @param limit
     * @param sex
     * @param username
     * @param mobile
     * @return
     */
    @Override
    public Result findYddUser(Integer page, Integer limit, Integer sex, String username, String mobile) {
        IPage<YddUser> pageInfo=new Page<>(page,limit);

        LambdaQueryWrapper<YddUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(sex!=null,YddUser::getSex,sex);
        wrapper.like(StrUtil.isNotBlank(username),YddUser::getUsername,username);
        wrapper.like(StrUtil.isNotBlank(mobile),YddUser::getMobile,mobile);

        pageInfo=page(pageInfo,wrapper);

        Map<String,Object> map=new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("currentPage",pageInfo.getCurrent());
        map.put("data",pageInfo.getRecords());
        map.put("pageSize",pageInfo.getSize());
        map.put("totalPages",pageInfo.getPages());

        return Result.ok(map);
    }

    /**
     * 添加用户
     * @param yddUser
     * @return
     */
    @Override
    public Result saveYddUser(YddUser yddUser) {
        yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));

        save(yddUser);

        return Result.ok(null);
    }

    /**
     * 修改操作
     * @param yddUser
     * @return
     */
    @Override
    public Result updateYddUser(YddUser yddUser) {
        updateById(yddUser);
        return Result.ok(null);
    }
}




